from error import Error
from interpreter import Interpreter, Context, RTResult
from lexer import Lexer
from parser import Parser, ParserResult


def run(fn, text):
    lexer = Lexer(fn, text)  # 创建词法分析器
    tokens, error = lexer.make_tokens()  # 获取分割的 token
    print("词法解析器生成的tokens: ", tokens)
    # 语法解析器解析成 AST 抽象语法树
    parse = Parser(tokens)
    ast: ParserResult = parse.parse()
    print("语法解析器生成的AST抽象语法树: ", ast.node)
    # 解释器获取结果
    interpreter = Interpreter()
    context = Context('<program>')
    res: RTResult = interpreter.visit(ast.node, context)
    return res.value, res.error
